package com.jzy.hzit.util;


import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R {
	private Integer code;
	private String message;
	private Map<String, Object> data;

	public R() {
		this.data = new HashMap<>();
	}

	// 1. 成功时的方法
	public static R ok() {
		R response = new R();
		response.setCodeAndMessage(ResponseEnum.SUCCESS);
		return response;
	}

	// 2. 失败时的调用方法
	public static R error() {
		R response = new R();
		response.setCodeAndMessage(ResponseEnum.ERROR);
		return response;
	}

	// 3. 设置其它的响应结果
	public static R setResult(ResponseEnum responseEnum) {
		R response = new R();
		response.setCodeAndMessage(responseEnum);
		return response;
	}

	// 辅助方法：设置code和message
	private void setCodeAndMessage(ResponseEnum responseEnum) {
		this.code = responseEnum.getCode();
		this.message = responseEnum.getMessage();
	}

	// 4. 修改出错信息
	public R message(String message) {
		this.message = message;
		return this;
	}

	// 5. 修改错误码
	public R code(Integer code) {
		this.code = code;
		return this;
	}

	// 6. 设置添加数据（单个键值对）
	public R data(String key, Object value) {
		this.data.put(key, value);
		return this;
	}

	// 7. 设置添加数据（整个Map）
	public R data(Map<String, Object> map) {
		if (map != null) {
			this.data.putAll(map);
		}
		return this;
	}

	// Getter 和 Setter 方法
	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data != null ? data : new HashMap<>();
	}
}


